﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LightContracts;
using NUnit.Framework;

namespace LightContractsTests
{
    [TestFixture]
    public class EnsuresTest
    {
        [Test]
        public void EnsuresTrue()
        {
            Ensures.That(true, "desc");
        }

        [Test]
        public void EnsuresIsNotNullTrue()
        {
            Ensures.IsNotNull(new object(), "name");
        }

        [Test]
        public void EnsuresFalse()
        {
            try
            {
                Ensures.That(false, "desc");
                Assert.Fail("This code should not be reached.");
            }
            catch (AssertException exception)
            {
                Assert.AreEqual("Postcondition failed. The expectation was 'desc', but this is false.", exception.Message);
            }
        }

        [Test]
        public void EnsuresFalseWithNamedMethod()
        {
            try
            {
            	Ensures.That(IsInit);
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Postcondition failed. The expectation was 'IsInit', but this is false.", exception.Message);
            }
        }

        private bool IsInit()
        {
        	return false;	
        }

        
        [Test]
        public void EnsuresFalseWithAnonymousMethod()
        {
            try
            {
            	Ensures.That(delegate(){return false;});
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Postcondition failed. The expectation was 'anonymous function delivers true', but this is false.", exception.Message);
            }
        }

        [Test]
        public void EnsuresFalseWithLambdaExpression()
        {
            try
            {
            	Ensures.That(()=> false);
                Assert.Fail("This code should not be reached.");
            }
            catch(AssertException exception)
            {
                Assert.AreEqual("Postcondition failed. The expectation was 'anonymous function delivers true', but this is false.", exception.Message);
            }
        }

        [Test]
        public void EnsuresIsNotNullFalse()
        {
            try
            {
                Ensures.IsNotNull(null, "name");
                Assert.Fail("This code should not be reached.");
            }
            catch (AssertException exception)
            {
                Assert.AreEqual("Postcondition failed. The expectation was 'name is not null', but this is false.", exception.Message);
            }
        }
    }
}
